package cn.tedu.confirm;

import com.rabbitmq.client.*;
import com.rabbitmq.client.AMQP.BasicProperties;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws Exception{
        //1.通过连接工厂配置rabbitmq的连接信息
        ConnectionFactory cf = new ConnectionFactory();
        cf.setHost("127.0.0.1");
        cf.setPort(5672); //客户端端口，5672是默认端口，可以省略
        cf.setUsername("guest");
        cf.setPassword("guest");
        //2.创建tcp和channel
        Connection nc = cf.newConnection();
        Channel cc = nc.createChannel();

        //消费数据
        DefaultConsumer consumer =  new DefaultConsumer(cc){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       BasicProperties properties,byte[] body) throws IOException {
                System.out.println("消息的回调函数");
                //表示确认了
//                cc.basicAck(envelope.getDeliveryTag(), false);
                //也可以返回拒绝接收该消息
                cc.basicReject(envelope.getDeliveryTag(),false);
            }
        };
        cc.basicConsume("helloworld",false,consumer);
    }
}
